// 封装 axios 请求响应拦截器封装
import axios from "axios";
import store from '@/store'
import { Message } from "_element-ui@2.15.2@element-ui";


var instance = axios.create({
    baseURL: 'http://localhost:8080',
    timeout: 300000,
});

instance.interceptors.request.use(config => {
    store.dispatch('GET_ACCESS_TOKEN')
    if (store.getters.token) {
        config.headers['Authorization'] = 'bearer ' + store.getters.token
    }
    store.dispatch('getAppToken')
    store.dispatch('getUserToken')
    if (store.getters.appToken) {
        config.headers['userToken'] = store.getters.userToken
        config.headers['appToken'] = store.getters.appToken
    }
    if (config.method === 'post' || config.method.method === 'put') {
        if (config.url == '/RAC/oauth/token') {
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
            config.data = qs.stringify(config.data)
        }
    } else if (config.method === 'get' || config.method === 'delete') {
        if (config.method === 'delete') {
            if (config.url.indexOf('/tags') != -1) {
                config.headers['Context-Type'] = 'application/json'
            } else {
                config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
                config.data = qs.stringify(config.data)
            }
        } else {
            config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
            config.data = qs.stringify(config.data)
        }
    }
    if (config.method === 'put') {
        if (config.url.indexOf('/properties') != -1) {
            config.headers['Content-Type'] = 'application/json'
        }
    }
    if (config.method === "patch") {
        if (config.url.indexOf('/tags') != -1) {
            config.headers['Content-Type'] = 'application/json'
        }
    }
    return config
}, error => {
    return Promise.reject(error)
})


instance.interceptors.response.use(function(response) {
    if (response.status != 200) {
        Message({
            message: response.data.msg,
            type: 'warning'
        });
    }

    return response;
}, async function(error) {
    if (error.response) {
        switch (error.response.status) {
            case 401:
                let fullurl = window.location.href
                let index = fullurl.indexOf('/#')
                let length = fullurl.substring(index).length

                if (length == 3) {
                    Message({
                        showClose: true,
                        message: '用户名或密码错误,请刷新浏览器后重新填写正确的用户名和密码',
                        type: 'error'
                    });
                }

                let res = await doRequest(error)
                if (res) {
                    return res;
                }
                break;

            case 400:
                Message({
                    showClose: true,
                    message: error.response.data.responseStatus.message,
                    type: 'warning'
                });
                break;
            case 500:
                break;
            default:
                Message({
                    showClose: true,
                    message: '连接服务器失败',
                    type: 'warning'
                })
        }
    }

    return Promise.reject(error)
})